<?php

namespace frontend\modules\shop\models;

use Yii;
use yii\data\ActiveDataProvider;

/**
 * This is the model class for table "{{%dynamic}}".
 *
 * @property int $id
 * @property string $user_id 用户ID
 * @property string $content
 * @property int $direction 动态类型：0主动动态，1被动动态
 * @property int $retweet_id 转发动态原始ID
 * @property int $status
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 */
class CategoryProductSearch extends \common\models\shop\ShopProduct {

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $id
     *
     * @return ActiveDataProvider
     */
    public function search($id) {
        
        $model = \common\models\shop\ShopCategory::find()->where(['id' => $id])->one();

        return $model->products;
        $query = static::find()->where(['id' => $id])->with('products');

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        Yii::$app->db->cache(function($db) use($dataProvider) {
            $dataProvider->prepare();
        }, 60);

        return $dataProvider;
    }

    public function getStudents() {
        return $this->hasMany(VUser::className(), ['id' => 'user_id'])->viaTable('vw_course_enrollment', ['course_id' => 'id'], function ($query) {
            $query->onCondition(['enroll_at' => 0]);
        });
    }

}
